package com.example.filesave;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.DropBoxManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.filesave.sql.Book;
import com.example.filesave.sql.MyOpenHelper;
import com.example.filesave.util.BaseActivity;
import com.example.filesave.util.L;

import java.util.ArrayList;

public class FileRWX extends BaseActivity {

    MyOpenHelper openHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);

        openHelper = new MyOpenHelper(this, "BookStore.db", null, 1);
        Button btn = (Button) findViewById(R.id.create_database);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openHelper.getWritableDatabase();
                Toast.makeText(FileRWX.this,"表创建成功了",Toast.LENGTH_LONG).show();
            }
        });

        Button btn2 = (Button) findViewById(R.id.add_data);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();

                // 开始组装数据
                contentValues.put("name","Thinking in java");
                contentValues.put("author","外国人");
                contentValues.put("pages",520);
                contentValues.put("price",160.2);
                db.insert("book",null,contentValues);
                contentValues.clear();
                // 开始组装数据
                contentValues.put("name","php Action");
                contentValues.put("author","外国人");
                contentValues.put("pages",521);
                contentValues.put("price",160.2);
                db.insert("book",null,contentValues);
                Toast.makeText(FileRWX.this,"插入数据成功",Toast.LENGTH_LONG).show();
            }
        });

        Button btn3 = (Button) findViewById(R.id.update);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();

                contentValues.put("price",10.333);
                db.update("book",contentValues,"name=?",new String[] {"php Action"});

                Toast.makeText(FileRWX.this,"修改成功",Toast.LENGTH_LONG).show();
            }
        });

        Button del = (Button) findViewById(R.id.delete);
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();

                db.delete("book","pages>?",new String[] {"510"});

                Toast.makeText(FileRWX.this,"删除成功",Toast.LENGTH_LONG).show();
            }
        });

        Button select = (Button) findViewById(R.id.select);
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = openHelper.getWritableDatabase();

               // 查询book中的所有的数据
                Cursor book = db.query("book", null, null, null, null, null, null);
                ArrayList<Book> books = new ArrayList<>();
                if (book.moveToFirst()){
                    do {
                        // 遍历
                        Integer id = Integer.valueOf(book.getString(book.getColumnIndex("id")));
                        String name = book.getString(book.getColumnIndex("name"));
                        String author = book.getString(book.getColumnIndex("author"));
                        Integer pages = Integer.valueOf(book.getString(book.getColumnIndex("pages")));
                        double price = Double.parseDouble(book.getString(book.getColumnIndex("price")));
                        L.d(id +"---"+name+"---"+author+"---"+pages+"---"+price);
                        books.add(new Book(  id,name,pages,price, author));
                    }while (book.moveToNext());
                }
                LinearLayout ly = (LinearLayout) findViewById(R.id.a);
                for (Book b: books) {
                    // 创建文本框
                    TextView textView = new TextView(FileRWX.this);
                    textView.setText(b.toString());
                    textView.setTextSize(25);
                    // 设置为linearLayout的子节点
                    ly.addView(textView);
                }
//                Toast.makeText(FileRWX.this,"查找数据"+books.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }
}
